//
//  NSData+Extension.swift
//  VISUAL
//
//  Created by 刘森 on 2019/12/27.
//  Copyright © 2019 admin. All rights reserved.
//

import Foundation
import UIKit

extension Data{
    /// 从本地读取w文件 ==> 生成data数据
    static func loadFileInLocal(fileName: String?, ofType: String) -> Data? {
        guard fileName != nil else {
            return nil
        }
        let path = Bundle.main.path(forResource: fileName, ofType: ofType)
        guard path != nil else {
            print(fileName! + "does not exist")
            return nil
        }
        var data: Data = Data.init()
        do {
            data = try Data.init(contentsOf: URL.init(fileURLWithPath: path!))
        } catch {
            print(error)
        }
        return data
    }
    
    /// 将data图片数据转换成image
    func arrayValue() -> NSArray? {
        let gifDataSource: CGImageSource = CGImageSourceCreateWithData(self as CFData, nil)!
        let gifImageCount: NSInteger = CGImageSourceGetCount(gifDataSource)
        let images: NSMutableArray   = NSMutableArray.init()
        for index in 0...gifImageCount-1 {
            let imageref: CGImage? = CGImageSourceCreateImageAtIndex(gifDataSource, index, nil)
            let image: UIImage = UIImage.init(cgImage: imageref!, scale: UIScreen.main.scale, orientation:.up)
            images.add(image)
        }
        return images
    }
}
